node-async-loop
Loop through an array to execute asynchronous actions on each element.
Sometimes you must execute an asynchronous action on each elements of an array, but you must wait for the previous
action to complete before proceed to the next.
Features:
- Loop through arrays
- Loop through objects
- Loop in the normal direction or in the reverse direction
- Error handling
- A callback is called for the end of the loop
Install
npm install --save node-async-loop
Function
var asyncLoop = require('node-async-loop');
asyncLoop(array[, from[, to]], callback[, endCallback]);
array: array
The array to loop
from (optionnal): integer
The starting position, including (Default: 0).
to (optionnal): integer
The final position, including (Default: array.length - 1).
callback: function(item, next)
The function called for every elements.
It must call next()
so that the next array element is executed.
At the end endCallback
will be called!
On error it must call next(errorObject)
and iteration will be stopped and the endCallback called with errorObject.
endCallback (optionnal): function(err)
This function is called at the end.
The err
variable is null if everything was fine, otherwise it contains the error.
Usage
Basic Usage
General usage:
var asyncLoop = require('node-async-loop');
var array = ['item0', 'item1', 'item2'];
asyncLoop(array, function (item, next)
{
do.some.action(item, function (err)
{
if (err)
{
next(err);
return;
}
next();
});
}, function (err)
{
if (err)
{
console.error('Error: ' + err.message);
return;
}
console.log('Finished!');
});
For example, create folder recursively:
var fs = require('fs');
var asyncLoop = require('node-async-loop');
var directories = ['test', 'test/hello', 'test/hello/world'];
asyncLoop(directories, function (directory, next)
{
fs.mkdir(directory, function (err)
{
if (err)
{
next(err);
return;
}
next();
});
}, function (err)
{
if (err)
{
console.error('Error: ' + err.message);
return;
}
console.log('Finished!');
});
Loop Partially and in reverse order!
var asyncLoop = require('node-async-loop');
var displayItem = function(item, next) {
console.log(item);
next();
}
var array = ['A', 'B', 'C', 'D', 'E', 'F'];
asyncLoop(array, displayItem);
asyncLoop(array, 0, displayItem);
asyncLoop(array, 0, array.length - 1, displayItem);
asyncLoop(array, 1, displayItem);
asyncLoop(array, 2, displayItem);
asyncLoop(array, 1, 3, displayItem);
asyncLoop(array, 0, 1, displayItem);
asyncLoop(array, 0, 2, displayItem);
asyncLoop(array, 3, 1, displayItem);
asyncLoop(array, 1, 0, displayItem);
asyncLoop(array, 2, 0, displayItem);
asyncLoop(array, 0, -2, displayItem);
asyncLoop(array, 1, -2, displayItem);
asyncLoop(array, array.length - 1, 0, displayItem);
asyncLoop(array, -1, 0, displayItem);
asyncLoop(array, -1, displayItem);
asyncLoop(array, -2, displayItem);
asyncLoop(array, -2, -4, displayItem);
asyncLoop(array, -4, -2, displayItem);
Loop through objects
var asyncLoop = require('node-async-loop');
var obj = {
'aa': 'AAAA',
'bb': 'BBBB',
'cc': 'CCCC',
'dd': 'DDDD',
'ee': 'EEEE'
};
asyncLoop(obj, function (item, next)
{
console.log(item);
next();
}, function ()
{
console.log('Finished!');
});